<?php

class ModelCfGallery extends Model {
    
    /**
     * Add new record
     *
     * @param       array       $data
     * @return      void
     * @access      public
     */
	public function addGallery($data)
    {
        $this->db->query('INSERT INTO ' . DB_PREFIX . 'gallery SET status = ' . (int)$data['status'] . ', sort_order = ' . (int)$data['sort_order'] . 
                         ', date_modified = NOW(), date_added = NOW()');
                         
        $news_id = $this->db->getLastId();
        
        if (isset($data['image'])) {
            $sql = sprintf("UPDATE %sgallery SET image = '%s' WHERE gallery_id = '%s'", DB_PREFIX, $this->db->escape($data['image']), (int)$news_id);            
            $this->db->query($sql);
        }
        
        foreach ($data['gallery'] as $language_id => $value) {
            $sql = sprintf("INSERT %sgallery_description SET gallery_id = %s, language_id = %s, title = '%s', description = '%s'", 
                           DB_PREFIX, $news_id, (int)$language_id, $this->db->escape($value['title']), $this->db->escape($value['description']));
                           
            $this->db->query($sql);
        }
	}
	
    // --------------------------------------------------------------------
    
    /**
     * Edit data
     *
     * @param       int         $gallery_id
     * @param       array       $data
     * @return      void
     * @access      public
     */
	public function editGallery($gallery_id, $data) 
    {
        $this->db->query('UPDATE ' . DB_PREFIX . 'gallery SET status = ' . (int)$data['status'] . ', date_modified = NOW() WHERE gallery_id = ' .(int)$gallery_id);
                                                                                              
        if (isset($data['image'])) {
            $this->db->query("UPDATE " . DB_PREFIX . "gallery SET image = '" . $this->db->escape($data['image']) . "' WHERE gallery_id = " . (int)$gallery_id);
        }
        
		$this->db->query("DELETE FROM " . DB_PREFIX . "gallery_description WHERE gallery_id = " . (int)$gallery_id);
        
        foreach ($data['gallery'] as $language_id => $value) {
            $sql = sprintf("INSERT %sgallery_description SET gallery_id = %s, language_id = %s, title = '%s', description = '%s'", 
                           DB_PREFIX, $gallery_id, (int)$language_id, $this->db->escape($value['title']), $this->db->escape($value['description']));
            $this->db->query($sql);
        }
	}
	
    // --------------------------------------------------------------------
    
    /**
     * Delete data
     *
     * @param       int     $gallery_id
     * @return
     * @access
     */
	public function deleteGallery($gallery_id) 
    {        
		$this->db->query("DELETE FROM " . DB_PREFIX . "gallery WHERE gallery_id = " . (int)$gallery_id);
        $this->db->query("DELETE FROM " . DB_PREFIX . "gallery_description WHERE gallery_id = " . (int)$gallery_id);
        $this->db->query("DELETE FROM " . DB_PREFIX . "gallery_image WHERE gallery_id = " . (int)$gallery_id);
	}
	
    // --------------------------------------------------------------------
    
    /**
     * Get basic gallery info.
     *
     * @param       int     $gallery_id
     * @return      array
     * @access      public
     */
	public function getGallery($gallery_id) 
    {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "gallery WHERE gallery_id = " . (int)$gallery_id);
		
        return $query->row;
	}

    // --------------------------------------------------------------------
    
    /**
     * Get galleries list
     *
     * @param       array       $data
     * @return      array
     * @access      public
     */
	public function getGalleries($data = array()) 
    {
        $sql = 'SELECT * FROM ' . DB_PREFIX . 'gallery g LEFT JOIN ' . DB_PREFIX . 'gallery_description gd 
                ON g.gallery_id = gd.gallery_id WHERE gd.language_id = ' . DEFAULT_LANG; 
		
        $sort_data = array('title', 'date_added');    
        
        $sql .= isset($data['sort']) && in_array($data['sort'], $sort_data) 
                ? " ORDER BY " . $data['sort'] : " ORDER BY date_added";    
        
        $sql .= isset($data['order']) && ($data['order'] == 'DESC') ? " DESC" : " ASC";
        
        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }    
            
            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }    
        
        $query = $this->db->query($sql);																																				
		
		return $query->rows;	
	}
	
    // --------------------------------------------------------------------
    
    /**
     * Get all images
     *
     * @param       int     $gallery_id
     * @return      array
     * @access      public
     */
    public function getAllImages($gallery_id) 
    {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "gallery_image WHERE gallery_id = " . (int)$gallery_id);
        
        return $query->rows;
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Get full gallery info
     *
     * @param       int     $gallery_id
     * @return      array
     * @access      public
     */
    public function getGalleryDetail($gallery_id)
    {
        $gallery_detail_data = array();
        
        $query = $this->db->query('SELECT * FROM ' . DB_PREFIX . 'gallery_description WHERE gallery_id = ' . (int)$gallery_id);
        
        foreach ($query->rows as $result) {
            $gallery_detail_data[$result['language_id']] = array(
                'title'         => $result['title'],
                'description'   => $result['description']
            );
        }
        
        return $gallery_detail_data;
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Count gallery
     *
     * @return      int
     * @access      public
     */
	public function getTotalNews() 
    {
		$query = $this->db->query('SELECT COUNT(*) AS total FROM ' . DB_PREFIX . 'gallery');
		
		return $query->row['total'];
	}
    
    // --------------------------------------------------------------------
    
    /**
     * Save image 
     *
     * @param       int     $gallery_id
     * @param       int     $image_id
     * @param       string  $image_name
     * @return      int
     * @access      public
     */
    public function saveImage($gallery_id, $image_id, $image_name)
    {
        $sql = sprintf('REPLACE INTO %sgallery_image SET gallery_id = %s, image_id = %s, image = \'%s\'', 
                       DB_PREFIX, (int)$gallery_id, (int)$image_id, $this->db->escape($image_name));
        $this->db->query($sql);
        
        return $this->db->getLastId();
    }
	
    // --------------------------------------------------------------------
    
    /**
     * Delete image 
     *
     * @param       int     $image_id
     * @return      void
     * @access      public
     */
    public function deleteImage($image_id)
    {
        $this->db->query('DELETE FROM ' . DB_PREFIX . 'gallery_image WHERE image_id = ' . (int)$image_id);
    }
    
}
?>